package com.etime.shycourse.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.pojo.Sysbanner;
import com.etime.shycourse.services.interfaces.SysbannerServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wb
 * @since 2022-01-04
 */
@Api(tags = "banner的数据管理接口")
@RestController
@RequestMapping("/sys/banner")
public class SysbannerController {
    @Autowired
    SysbannerServices services;

    @ApiOperation("1、增加banner功能 2、修改 Sysbanner根据 bannerid")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "有默认值 bannerstate = 1 (默认可用)", name = "bannerstate"),
            @ApiImplicitParam(value = "有默认值 bannersort = 3 (默认排序为3)", name = "bannersort"),
            @ApiImplicitParam(value = "有默认值 bannerlinktype = 1 (表示为课程)", name = "bannerlinktype"),
    })
    @PostMapping("/insert")
    public BaseMessage insertOrUpdateSysBanner(Sysbanner sysbanner) {
        int rtn = services.insertOrUpdateSysBanner (sysbanner);
        BaseMessage msg = new BaseMessage ();
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("增加banner或修改成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("增加banner或修改失败");
        }
        return msg;
    }

    @ApiOperation("banner分页查询列表 可使bannerId查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bannerId", value = "bannerId 选填条件bannerId"),
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少行"),
    })
    @GetMapping("/page")
    public PageMessage<Sysbanner> selectSysBannerPage(Integer bannerId, Long pageNum, Long pageSize) {
        PageMessage<Sysbanner> msg = new PageMessage<> ();
        Page<Sysbanner> data = services.selectSysBannerPage (bannerId, pageNum, pageSize);
        if (data != null) {
            msg.setCode (1);
            msg.setPagedata (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("根据bannerid删除banner成功")
    @PostMapping("/delete/{bannerId}")
    public BaseMessage deleteSysBannerById(@PathVariable Integer bannerId) {
        int rtn = services.deleteById (bannerId);
        BaseMessage msg = new BaseMessage ();
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("删除banner成功");
        } else {
            msg.setMsg ("删除banner失败");
        }
        return msg;
    }
}

